package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.QingSyncOrderDao;
import com.wonder.app.mapper.QingSyncOrderDetailDao;
import com.wonder.app.mapper.SyncOrderDao;
import com.wonder.app.mapper.SyncOrderDetailDao;
import com.wonder.app.request.QingSyncOrderRequest;
import com.wonder.app.request.SyncOrderRequest;
import com.wonder.app.service.*;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class QingSyncOrderServiceImpl extends ServiceImpl<QingSyncOrderDao, QingSyncOrder> implements QingSyncOrderService {

    @Autowired
    private QingSyncOrderDetailService qingSyncOrderDetailService;

    @Autowired
    private QingSyncOrderDetailDao qingSyncOrderDetailDao;

    @Autowired
    private QingSyncOrderDao qingSyncOrderDao;

    @Override
    public IPage<QingSyncOrder> selectList(QingSyncOrderRequest request, Page<QingSyncOrder> page) {
        LambdaQueryWrapper<QingSyncOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!VtuzxUtil.isEmpty(request.getBillNo())){
            lambdaQueryWrapper.like(QingSyncOrder::getBillNo,request.getBillNo());
        }

        if (!VtuzxUtil.isEmpty(request.getUserName())){
            lambdaQueryWrapper.like(QingSyncOrder::getUserName,request.getUserName());
        }
        if (request.getStartTime() != null){
            Date[] orderDate = request.getStartTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(QingSyncOrder::getStartTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(QingSyncOrder::getStartTime, DateUtils.dateToStr(orderDate[1],"yyyy-MM-dd")+ " 23:59:59");

        }
        lambdaQueryWrapper.orderByDesc(QingSyncOrder::getId);
        Page<QingSyncOrder> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    @Override
    public String startQingSyncOrder(String name, Long userId, String type) {

        String billNo = IXqcConst.createBillNo(BillType.DDTB);
        QingSyncOrder qingSyncOrder = new QingSyncOrder();
        qingSyncOrder.setStartTime(new Date());
        qingSyncOrder.setBillNo(billNo);
        qingSyncOrder.setType(type);
//        qingSyncOrder.setUserId();
        qingSyncOrder.setUserName(name);
        qingSyncOrderDao.insert(qingSyncOrder);
        return billNo;
    }

    @Override
    public Map<String,Object> endQingSyncOrder(List<QingSyncOrderDetail> orderDetailList, String tbbillNo) {
        qingSyncOrderDetailService.saveOrUpdateBatch(orderDetailList);
        int successCount = 0;
        int failCount = 0;
        for (QingSyncOrderDetail qingSyncOrderDetail : orderDetailList){
            Boolean isSuccess = qingSyncOrderDetail.getIsSuccess();
            if (isSuccess){
                successCount ++;
            } else {
                failCount ++;
            }
        }
        QingSyncOrder qingSyncOrder = qingSyncOrderDao.selectOne(new LambdaQueryWrapper<QingSyncOrder>()
                .eq(QingSyncOrder::getBillNo,tbbillNo));
        qingSyncOrder.setCount(orderDetailList.size());
        qingSyncOrder.setFailCount(failCount);
        qingSyncOrder.setSuccessCount(successCount);
        qingSyncOrder.setEndTime(new Date());
        qingSyncOrderDao.updateById(qingSyncOrder);
        Map<String,Object> data = new HashMap<>();
        data.put("successCount",successCount);
        data.put("failCount",failCount);
        return data;
    }

    @Override
    public Map<String, Object> dumpdata(Map<String, Object> map) {
        String billNo = map.get("billNo").toString();
        List<QingSyncOrderDetail> orderDetailList = qingSyncOrderDetailDao
                .selectList(new LambdaQueryWrapper<QingSyncOrderDetail>().eq(QingSyncOrderDetail::getBillNo, billNo));
        List<Map<String,Object>> dataList = new ArrayList<>();
        //整理一下需要的数据
        for (QingSyncOrderDetail qingSyncOrderDetail : orderDetailList){
            Boolean isSuccess = qingSyncOrderDetail.getIsSuccess();
            String orderNo = qingSyncOrderDetail.getOrderNo();
            String message = qingSyncOrderDetail.getMessage();

            Map<String,Object> orderMap = new LinkedHashMap<>();
            orderMap.put("orderNo",orderNo);

            if (isSuccess){
                orderMap.put("isSuccess","成功");
            } else {
                orderMap.put("isSuccess","失败");
            }
            orderMap.put("message",message);
            dataList.add(orderMap);
        }
        String header = "订单编号,是否成功,错误信息";
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("header",StringUtils.split(header,','));
        retMap.put("filename", "订单同步.xlsx");
        retMap.put("percentage", dataList.size());
        retMap.put("data", dataList);
        return AjaxMap.success(retMap);
    }
}
